package com.project.service.impl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class BaseService {
    /**封装配置信息的会话工厂*/
    private static SqlSessionFactory factory;
    static{
        try {
            //相对于class文件，得到主配置文件的读取流
            Reader r = Resources.getResourceAsReader("mybatis.cfg.xml");
            factory = new SqlSessionFactoryBuilder().build(r);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建并返回会话，一个会话就相当于一个连接
     * @return 会话对象
     */
    public SqlSession getSession(){
        return factory.openSession();
    }
}
